﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using V5_Common;

namespace V5_EShop.Admin.Picture
{
    public partial class UploadLogo : System.Web.UI.Page
    {
        string newPath = "logo";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.FileName == "")
            {
                Js.Alert("请选择一个图片!");
                return;
            }
            string fileName = Path.GetFileName(this.FileUpload1.FileName);
            string extension = Path.GetExtension(this.FileUpload1.FileName);
            string filePath = DateTime.Now.ToString("yyyyMMdd");
            string path1 = Server.MapPath("~/UpLoadFiles/" + newPath + "/");
            if (!Directory.Exists(path1))
            {
                Directory.CreateDirectory(path1);
            }
            string fileName1 = DateRndName();
            //path1 = path1 + fileName1 + extension;
            path1 = path1 + fileName;
            string path2 = "/UpLoadFiles/" + newPath + "/" + fileName;
            try
            {
                this.FileUpload1.SaveAs(path1);
                this.ClientScript.RegisterStartupScript(this.GetType(), "upload", "<script>returnValue('" + path2 + "');</script>");
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// 日期
        /// </summary>
        /// <returns></returns>
        private string DateRndName()
        {
            DateTime d = DateTime.Now;
            string s = null, y, m, dd, h, mm, ss;
            y = d.Year.ToString();
            m = d.Month.ToString();
            if (m.Length < 2) m = "0" + m;
            dd = d.Day.ToString();
            if (dd.Length < 2) dd = "0" + dd;
            h = d.Hour.ToString();
            if (h.Length < 2) h = "0" + h;
            mm = d.Minute.ToString();
            if (mm.Length < 2) mm = "0" + mm;
            ss = d.Second.ToString();
            if (ss.Length < 2) ss = "0" + ss;
            s += y + m + dd + h + mm + ss;
            return s;
        }
    }
}
